import instAndTaskShareApi from "@/api/instAndTaskShareApi";

export default {
    computed: {
    },
    props:[""],
    data() {
        return {
            treeRootList: [],
            initAppManagerEnd: false,
            isShowManageTree: false,
            isManageToTree: false,
            isRootInit: false,
            mobileAppId: uni.getStorageSync("mobileAppId") || "",
            user:{}
        }
    },
    created() {
        this.user = uni.getStorageSync("user")
        if (this.mobileAppId) {
            this.isShowManageTree = true;
            return;
        }
        // 判断是否是bpm客户端管理员
        if (this.isBpmClient) {
            this.initAppManagerByBpm();
        } else if (this.isMyBpm) {
            // 我的流程相关，{我的待办，我的已办，我的已办，我的已阅。。。 }统一设置为管理员
            this.initMyTaskApp();
        } else {
            // 判断是否是各个应该的管理员
            this.initAppManager();
        }
    },
    methods: {
        async initMyTaskApp() {
            let self_ = this;
            let params = {
                type: this.readType,
            };
            if(this.appId){
                params.appId=this.appId;
            }
            let res = await instAndTaskShareApi.getMyOwnBpmApp(params);
            if (res.success) {
                self_.treeRootList = res.data;
            }
            self_.initQueryParam();
            self_.isShowManageTree = true;
            this.init();
        },
        async initAppManagerByBpm() {
            let self_ = this;
            let params = {
                appTypes: "1",
                catKey: "BPM",
                readKey: self_.readKey
            };
            if(this.appId){
                params.appId=this.appId;
            }
            let res = await instAndTaskShareApi.getMyOwnApp(params);
            if (res.success) {
                self_.treeRootList = res.data;
            }
            self_.initQueryParam();
            self_.isShowManageTree = true;
            this.init();
        },
        async initAppManager() {
            let self_ = this;
            self_.treeRootList.push({
                appId: "defaultTree",
                name: "平台分类",
                isAdmin: false
            });
            let isAdmin = false;
            if ((this.user && this.user.admin) || this.isMyBpm) {
                this.treeRootList[0].isAdmin = true;
                isAdmin = true;
                this.isManageToTree = true;
            }
            let res = await instAndTaskShareApi.getByCategoryOrKey({CID: '-1'});
            for (let i = 0; i < res.data.length; i++) {
                let app = res.data[i];
                let obj = {
                    appId: app.appId,
                    name: self_.$i18(app.clientName, '应用名称'),
                    title: self_.$i18(app.clientName, '应用名称'),
                    isAdmin: isAdmin
                }
                self_.treeRootList.push(obj);
            }
            //超级管理员、平台管理员，拥有所有权限
            if (isAdmin) {
                self_.initQueryParam();
                self_.isShowManageTree = true;
                this.init();
                return;
            }
            //是否为某应用的管理员
            // 加载当前用户的应用管理员权限
            let managers = await instAndTaskShareApi.getManagerOfCurUser();
            if (managers.data.length == 0) {
                self_.initQueryParam();
                self_.isShowManageTree = true;
                this.init();
                return;
            }
            //循环匹配对应应用管理员权限数组
            for (let auth of managers.data) {
                let app = self_.treeRootList.find(item => item.appId === auth.appId);
                if (app.appId) {
                    app.isAdmin = true;
                }
            }

            self_.initQueryParam();
            self_.isShowManageTree = true;
            this.init();
        },
        initQueryParam() {
            if (this.queryParam) {
                this.queryParam['treeCatType'] = "allRootApp";
                this.queryParam['treeRootList'] = JSON.stringify(this.getAppList());
            }
            this.initAppManagerEnd = true;
        },
        getAppList() {
            let appList = {};
            for (let i = 0; i < this.treeRootList.length; i++) {
                let app = this.treeRootList[i];
                let obj = {
                    appId: app.appId,
                    isAdmin: app.isAdmin
                }
                appList[app.appId] = obj;
            }
            return appList;
        },
        getKeyVal(prefix, key) {
            if (prefix) {
                return "Q_" + prefix + key;
            }
            return "Q_" + key;
        },
        cancel() {
            let param = {
                treeCatType: "",
                treeRootList: JSON.stringify(this.getAppList())
            };
            let treeKey = this.getKeyVal(this.treePrefix, 'TREE_ID__S_EQ')
            let appKey = this.getKeyVal(this.appPrefix, 'APP_ID__S_EQ');
            if (this.queryParam.treeCatType ||
                this.queryParam[treeKey] ||
                this.queryParam[appKey]) {
                param.treeCatType = this.queryParam.treeCatType;
                param[treeKey] = this.queryParam[treeKey];
                param[appKey] = this.queryParam[appKey];
            }
            this.queryParam = {
                ...param
            };
            let appId = this.curApp.appId;
            if (this.hasAppId && appId) {
                this.queryParam.Q_APP_ID__S_EQ = appId;
            }
            this.$nextTick(() => {
                this.$refs.table.loadData();
            })
        },
        loadDataAgen() {
            if (!this.isInit) {
                return;
            }
            this.isInit = false;
            let param = {
                'treeCatType': 'allRootApp',
                'treeRootList': JSON.stringify(this.getAppList())
            };
            let treeKey = this.getKeyVal(this.treePrefix, 'TREE_ID__S_EQ');
            param[treeKey] = "";
            this.$emit('loadListByParams', param)
        },
        //获取筛选参数
        loadListShare() {
            if (!this.isShowManageTree) {
                return;
            }
            this.status = 'more';
            let params = {}
            let treeKey = this.getKeyVal(this.treePrefix, 'TREE_ID__S_EQ');
            let appKey = this.getKeyVal(this.appPrefix, 'APP_ID__S_EQ');
            params[appKey] = "";
            params['treeCatType'] = "";
            params['treeRootList'] = "";
            let _name = this.screenName? this.screenName:'NAME';

            if (this.searchVal) {
                this.$set(params, this.getKeyVal(this.treePrefix, _name+'__S_LK'), this.searchVal);
            }
            if (this.parentId) {
                this.$set(params, treeKey, this.parentId)
            }
            if (!this.mobileAppId) {
                let curRow = this.getSelectTree();
                let treeId = this.parentId;
                if (treeId != "0") {
                    params[treeKey] = curRow.treeId;
                } else {
                    params[treeKey] = '';
                    //顶级目录（包括平台与应用目录）
                    params['treeCatType'] = 'allRootApp';
                    params['treeRootList'] = JSON.stringify(this.getAppList());
                    this.$emit('loadListByParams', params)
                    return;
                }
                if ("rootApp" == curRow.treeCatType) {
                    params[treeKey] = '';
                    params.treeCatType = 'customApp';
                    params.treeRootList = JSON.stringify(this.getAppList());
                    if ("defaultTree" == curRow.appId) {
                        //顶级平台目录
                        params.treeCatType = 'defaultApp';
                    } else {
                        params[appKey] = curRow.appId;
                    }
                }
            }

            if (this.mobileAppId) {
                params[appKey] = this.mobileAppId;
            }
            debugger;
            this.$emit('loadListByParams', params)
        },
        getSelectTree() {
            if (this.headList.length == 1) {
                let tree = this.headList[0];
                if ("allRootApp" == tree.treeCatType) {
                    this.headList = [];
                }
                return tree;
            } else if (this.headList.length == 0) {
                this.initAllRootAppParams();
                let tree = this.headList[0];
                return tree;
            }
            let tree = this.headList.find(item => item.treeId == this.parentId);
            return tree;
        },
        initAllRootAppParams() {
            this.heralist = [];
            this.parentId = "0";
            let appObj = this.treeRootList.find(item => item.appId == 'defaultTree');
            if (!appObj) {
                appObj = {
                    name: "",
                    appId: ""
                }
            }
            var hering = {};
            this.$set(hering, 'name', appObj.name);
            this.$set(hering, 'parentId', "0");
            this.$set(hering, 'treeId', "0");
            this.$set(hering, 'appId', appObj.appId);
            this.$set(hering, 'treeCatType', "allRootApp");
            this.heralist.push(hering);
        },
    }
}
